define(function(require,exports,module){
	var number = null;
	var folderType = null;
	var pid = '';
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	var ajax = require("resource/js/component/ajax.js");
	var easyutil = require("resource/js/component/easyutil.js");
	var urltool = require("resource/js/component/urlTool.js");
	require("resource/js/component/rules.js");
	
	number = urltool.getParam("number");
	folderType = urltool.getParam("folderType");
	pid = urltool.getParam("pid");
	function loadViewEmail() {
		ajax.init().success(function(code,msg,data){
			if(code==0){
				$("#eTitle").text(data.subject);
				$("#eFrom").html(parseEmailList(data.fromAddressList));
				$("#eTo").html(parseEmailList(data.toAddressList));
				var emailDate = '';
				if (folderType == 1) {
					emailDate = data.receivedDate;
				} else {
					emailDate = data.sendDate;
				}
				$("#eSendData").text(emailDate);
				$("#eContent").html(data.emailText);
				$("#eFileName").text(data.fileName);
				var otherAddressHtml = '';
				if (data.ccAddressList != null && data.ccAddressList.length > 0) {
					otherAddressHtml += '<li class="top-file-block">'
						+ '<div class="iI0">抄&nbsp;&nbsp;&nbsp;送：</div>'
						+ '<div class="hX0">'+parseEmailList(data.ccAddressList)+'</div>'
						+ '</li>';
				}
				//不用显示密送
				/*if (data.bccAddressList != null && data.bccAddressList > 0) {
					otherAddressHtml += '<li class="top-file-block">'
						+ '<div class="iI0">密&nbsp;&nbsp;&nbsp;送：</div>'
						+ '<div class="hX0">'+parseEmailList(data.bccAddressList)+'</div>'
						+ '</li>';
				}*/
				if (otherAddressHtml != '') {
					$("#eToBlock").after(otherAddressHtml);
				}
				//第一次读取且需要回复
				if (data.replyTo) {
					$(".reply-block").show();
				}
				if (data.fileList != null && data.fileList.length >0) {
					var html = '';
					var fileListHtml = '';
					for(var i = 0; i < data.fileList.length; i ++) {
						html += '(<a style="text-decoration: none;" href="'+BASEPATH+'/oa/email/downloadFile.htm?number='+number+'&folderType='+folderType+'&fileName='+data.fileList[i].fileName+'&fileId='+data.fileList[i].fileId+'">'+data.fileList[i].fileName+'</a>);';
						fileListHtml += '<div class="file-block">'
							+ '<div class="icon-img">'
							+ '<img style="width:auto;" src="'+BASEPATH+'/resource/images/email/fu_rar.gif"></div>'
							+ '<div class="file-detail-block">'
							+ '<div class="file-name" title="'+data.fileList[i].fileName+'">'+data.fileList[i].fileName+'</div>'
							+ '<div><a href="'+BASEPATH+'/oa/email/downloadFile.htm?number='+number+'&folderType='+folderType+'&fileName='+data.fileList[i].fileName+'&fileId='+data.fileList[i].fileId+'">下载</a></div>'
							+ '</div></div>';
					}
					$("#eFileName").html(html);
					$(".file-inner").html(fileListHtml);
					$(".file-list-block").show();
					$(".top-file-block").show();
				}
				if (folderType == 1) {//收件箱
					$("#replyPanel").show();
				}
				if (folderType != 1 && data.fileList.length == 0) {
					$("#footerPanel").hide();
				}
			}
		}).post(BASEPATH + "/oa/email/getViewEmail.htm",{number: number,folderType: folderType},'get');
	}
	
	function parseEmailList(emailList) {
		var addr = '';
		if(emailList){
			for (var i = 0; i < emailList.length; i++) {
				var name = '';
				var email = '';
				var canadd = false;
				if (emailList[i].personal != null && emailList[i].personal != "") {
					name = emailList[i].personal
				} 
				if (emailList[i].email) {
					email = emailList[i].email;
				}
				if (emailList[i].canadd == 'true') {
					canadd = true;
				}
				addr += addrHtmlTmp(name, email, canadd);
			}
			return addr;
		}
	}
	
	var addrHtmlTmp = function(name, email, hasAdd) {
		var addBtn = '';
		var hasAddClass = '';
		if (hasAdd) {
			hasAddClass = 'nui-addr-hasAdd';
			addBtn = '<a href="javascript:void(0)" class="nui-addr-add" title="添加联系人">+</a>';
		}
		var addrHtml = '<div class="e-email-block" data-e-name="'+name+'" data-e-email="'+email+'">'
			+ '<div class="nui-editableAddr nui-addr '+hasAddClass+'" unselectable="on">'
			+ '<strong class="nui-addr-name" unselectable="on">'+name+'</strong>'
			+ '<em class="nui-addr-email" unselectable="on">&lt;'+email+'&gt;</em>'+addBtn+'</div></div>';
		return addrHtml;
	}
	//新增联系人显示添加组
	$("#adddlg").on('click', "#addGroup", function() { 
		$("#addGroupBlock").show();
		$("#addGroup").hide();
	}).on('click', '#cancelSaveGroup', function() { //新增联系人取消添加组
		$("#groupName").val('');
		$("#addGroupBlock").hide();
		$("#addGroup").show();
	}).on('click', '#saveGroup', function() { //新增联系人添加组
		var groupName = $("#groupName").val();
		if (groupName == null || groupName == '') {
			$.messager.show({
				title:'',
				msg: "组名不能为空",
				timeout:1000,
				showType:'slide'
			});
			return;
		}
		if (groupName.length > 100) {
			$.messager.show({
				title:'',
				msg: "组名长度必须介于0和100之间",
				timeout:1000,
				showType:'slide'
			});
			return;
		}
		ajax.init().success(function(code, msg, data){
			if (code == 0) {
				ajax.init().success(function(code, msg, data){
					if (code == 0) {
						initCombobox();
						$("#addGroupBlock").hide();
						$("#addGroup").show();
						$.messager.show({
							title:'',
							msg: msg,
							timeout:1000,
							showType:'slide'
						});
					}
				}).post(BASEPATH+'/oa/emailContacts/addContactsGroup.htm', {groupName: groupName});
			} else {
				$.messager.show({
					title:'',
					msg: msg,
					timeout:1000,
					showType:'slide'
				});
			}
		}).post(BASEPATH+'/oa/emailContacts/checkGroupName.htm', {groupName: groupName});
	});
	//初始化分组下拉框
	function initCombobox(){ 
		ajax.init().success(function(code, msg, data){
			data.unshift({
				groupId:"", groupName:"请选择"
			});
			$("#groupId").combobox("loadData",data);
		}).post(BASEPATH+'/oa/emailContacts/getContactsGroupList.htm');
	};
	//点击添加到联系人中
	$('.email-head').on('click', '.nui-addr-add', function() {
		var self = $(this);
		var name = self.parents(".e-email-block").data('e-name');
		var email = self.parents(".e-email-block").data('e-email');
		
		$("#adddlg").dialog({
			iconCls: 'icon-save',
			title:'新增联系人',
			href: BASEPATH + '/view/oa/email/contactsAdd',
			onLoad:function(){
				using("combobox",function(){
					$("#groupId").combobox({
						valueField : 'groupId',
						textField: 'groupName',
						prompt: '请选择分组',
						editable: false,
						required: false
					});
					initCombobox();
					$('#contactsName').textbox('setValue', name);
					$('#contactsMail').textbox('setValue', email);
				});
		    },
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#addform").form("validate")){
							//判断邮箱是否可用
							var contactsMail = $("#contactsMail").textbox("getValue");
							ajax.init().success(function(code, msg, data) {
								if (code == 0) {
									ajax.init().success(function(code,msg,data){
										$("#adddlg").dialog("close");
										alerts.show(msg);
									})
									.error(function(code,msg,data){
										alerts.alert(msg);
									}).post(BASEPATH+'/oa/emailContacts/addContacts.htm',$("#addform").serializeJson());
								} else {
									$.messager.show({
										title:'',
										msg:"邮箱已存在",
										timeout:1000,
										showType:'slide'
									});
								}
							}).post(BASEPATH+'/oa/emailContacts/checkContactsMail.htm', {contactsMail: contactsMail});
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#adddlg').dialog('close');
				}
			}]
		});
		$("#adddlg").dialog("open");
	});
	
	$("#replyMsgSend").click(function() { //回复
		var emailText = '您发送的这封邮件已经被打开。';
		var email = $("#eFrom .e-email-block").data('e-email');
		var personal = $("#eFrom .e-email-block").data('e-name')
		var toAddressList = [{personal: personal, email: email}];
		var param = {
				emailText: emailText,
				toAddressList: toAddressList,
				number: number,
				folderType: folderType
		};
		ajax.init().success(function(code,msg,data){
			if(code==0){
				$(".reply-block").hide();
			}
		}).post(BASEPATH + "/oa/email/replyEmail.htm", param);
	});
	//不发送
	$("#replyMsgNotSend").click(function(){ 
		$(".reply-block").hide();
	});
	
	//快速回复
	$("#replySend").click(function() {
		var replyContent = $("#replyContent").val();
		var toAddressList = [];
		toAddressList[0] = {
				personal: $("#eFrom .e-email-block").data('e-name'),
				email: $("#eFrom .e-email-block").data('e-email')
		};
		var param = {
				emailText: replyContent,
				toAddressList: toAddressList,
				number: number,
				folderType: folderType
		};
		if (replyContent == "") {
			$.messager.show({
				title:'',
				msg: "内容不能为空",
				timeout:1000,
				showType:'slide'
			});
			return;
		}
		if (toAddressList.length <= 0) {
			$.messager.show({
				title:'',
				msg: "发送地址不能为空",
				timeout:1000,
				showType:'slide'
			});
			return;
		}
		ajax.init().success(function(code,msg,data){
			if(code==0){
				$.messager.show({
					title:'',
					msg:msg,
					timeout:1000,
					showType:'slide'
				});
				$("#replyContent").val('');
			}
		}).post(BASEPATH + "/oa/email/replyEmail.htm", param);
	});
	//取消
	$("#replyCancel").click(function() {
		$("#replyContent").val('');
		$(".nui-ipt-input").css({
			'height' : '20px',
			'overflow' : 'hidden',
			'font-size' : '12px'
		});
		$(".textarea-prompt").show();
		$(".reply-btn-block").hide();
	});
	
	$("#textarea-block").click(function() {
		$(".nui-ipt-input").css({
			'height' : '90px',
			'overflow' : 'auto',
			'font-size' : '14px'
		});
		$(".textarea-prompt").hide();
		$(".reply-btn-block").show();
	});
	
	//删除邮件
	$(".deleteEmail").click(function() {
		ajax.init().success(function(code,msg,data){
			if(code==0){
				$.messager.show({
					title:'',
					msg:msg,
					timeout:1000,
					showType:'slide'
				});
				if (folderType == 1) {
					setTimeout(function() {
						window.location.href = BASEPATH + "/view/oa/email/inboxEmail?pid="+pid;
					}, 500);
				} else if (folderType == 2) {
					setTimeout(function() {
						window.location.href = BASEPATH + "/view/oa/email/sentEmail?pid="+pid;
					}, 500);
				}else if (folderType == 3) {
					setTimeout(function() {
						window.location.href = BASEPATH + "/view/oa/email/draftEmail?pid="+pid;
					}, 500);
				}else if (folderType == 4) {
					setTimeout(function() {
						window.location.href = BASEPATH + "/view/oa/email/recycleEmail?pid="+pid;
					}, 500);
				}
			}
		}).post(BASEPATH + "/oa/email/deleteEmail.htm", {number:number, folderType: folderType});
	});
	//返回
	$(".emailBack").click(function() {
		if (folderType == 1) {
			window.location.href = BASEPATH + "/view/oa/email/inboxEmail?pid="+pid;
		}else if (folderType == 2) {
			window.location.href = BASEPATH + "/view/oa/email/sentEmail?pid="+pid;
		}else if (folderType == 3) {
			window.location.href = BASEPATH + "/view/oa/email/draftEmail?pid="+pid;
		}else if (folderType == 4) {
			window.location.href = BASEPATH + "/view/oa/email/recycleEmail?pid="+pid;
		}
	});
	//完整模式回复
	$(".fullReply").click(function() {
		var replyContent = $("#replyContent").val();
		var toAddress = $("#eFrom .e-email-block").data('e-email');
		var personal = $("#eFrom .e-email-block").data('e-name')
		var subject = "回复：" + $("#eTitle").text();
		if(window.sessionStorage){
			window.sessionStorage.removeItem('oa_email_view_obj');
			var oaEmailViewObj = {
				emailContent: replyContent,
				personal: personal,
				toAddress: toAddress,
				subject: subject
			};
			window.sessionStorage.setItem('oa_email_view_obj', JSON.stringify(oaEmailViewObj));
		} else {
			alert('您的浏览器版本太低');
		}
		window.location.href = BASEPATH + "/view/oa/email/sendEmail?sendType=reply&pid="+pid+"&number="+number+"&folderType="+folderType;
//		window.location.href = BASEPATH + "/view/oa/email/sendEmail?sendType=reply&pid="+pid+"&number="+number+"&folderType="+folderType+"&emailContent="+encodeURI(encodeURI(replyContent))+"&personal="+encodeURI(encodeURI(personal))+"&toAddress="+toAddress+"&subject="+encodeURI(encodeURI(subject));
	});
	//转发
	$(".forward").click(function() {
		var replyContent = $("#replyContent").val();
		var subject = "转发：" + $("#eTitle").text();
		if(window.sessionStorage){
			window.sessionStorage.removeItem('oa_email_view_obj');
			var oaEmailViewObj = {
					subject: subject
				};
			window.sessionStorage.setItem('oa_email_view_obj', JSON.stringify(oaEmailViewObj));
		} else {
			alert('您的浏览器版本太低');
		}
		window.location.href = BASEPATH + "/view/oa/email/sendEmail?sendType=forward&pid="+pid+"&number="+number+"&folderType="+folderType;
//		window.location.href = BASEPATH + "/view/oa/email/sendEmail?sendType=forward&pid="+pid+"&number="+number+"&folderType="+folderType+"&subject="+encodeURI(encodeURI(subject));
		
	});
	
	function detectionHeight() {
		var evHeight = $('.ev').height();
		$("#contentDiv").css({'max-height': evHeight - 85});
	}
	
	module.exports = {
		init: function() {
			loadViewEmail();
			detectionHeight();
		}
	}
});
